package com.samphanie.auiu.biz_detail.biz.goods

import android.content.res.ColorStateList
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.android.material.chip.Chip
import com.samphanie.auiu.biz_detail.R
import com.samphanie.auiu.biz_detail.databinding.LayoutGoodsDetailItemCommentBinding
import com.samphanie.auiu.biz_detail.databinding.LayoutGoodsDetailItemCommentItemBinding
import com.samphanie.auiu.common.ui.view.loadCircle
import com.samphanie.auiu.library.utils.AuiuDisplayUtil
import com.samphanie.auiu.pub_mod.model.GoodsDetailModel
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder
import kotlin.math.min

/**
 * @author ZSY
 */
class GoodsCommentItem(val model: GoodsDetailModel) :
    AuiuDataItem<GoodsDetailViewModel, AuiuViewHolder>() {

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_item_comment
    }

    private lateinit var binding: LayoutGoodsDetailItemCommentBinding
    private lateinit var bindingItem: LayoutGoodsDetailItemCommentItemBinding

    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context ?: return
        // holder.findViewById<TextView>(R.id.comment_title)?.text = model.commentCountTitle


        binding.commentTitle.text = model.commentCountTitle
        val commentTag = model.commentTags
        if (commentTag.isNotBlank()) {
            val commentTags = commentTag.split(" ")
            if (commentTags.isNotEmpty()) {
                commentTags.forEachIndexed { index, value ->
                    val chipLabel = if (index < binding.chipGroup.childCount) {
                        binding.chipGroup.getChildAt(index) as Chip
                    } else {
                        val chipLabel = Chip(context)
                        chipLabel.chipCornerRadius = AuiuDisplayUtil.dp2px(4f).toFloat()
                        chipLabel.chipBackgroundColor = ColorStateList.valueOf(
                            ContextCompat.getColor(
                                context,
                                R.color.color_faf0
                            )
                        )
                        chipLabel.setTextColor(ContextCompat.getColor(context, R.color.color_999))
                        chipLabel.textSize = 14f
                        chipLabel.gravity = Gravity.CENTER
                        chipLabel.isCheckedIconVisible = false
                        chipLabel.isCheckable = false
                        chipLabel.isChipIconVisible = false

                        binding.chipGroup.addView(chipLabel)
                        chipLabel
                    }

                    chipLabel.text = value
                }
            }
        }

        model.commentModels?.let {
            val commentContainer = binding.commentContainer
            for (index in 0..min(it.size - 1, 3)) {
                val comment = it[index]
                bindingItem = LayoutGoodsDetailItemCommentItemBinding.inflate(LayoutInflater.from(context), commentContainer, false)
                commentContainer.addView(bindingItem.root)
//                if (index < commentContainer.childCount) {
//                    bindingItem.root = commentContainer.getChildAt(index)
//                } else {
//                    bindingItem = LayoutGoodsDetailItemCommentItemBinding.inflate(LayoutInflater.from(context), commentContainer, false)
//                    commentContainer.addView(bindingItem.root)
//                }
                bindingItem.userAvatar.loadCircle(comment.avatar)
                bindingItem.username.text = comment.nickname
                bindingItem.commentContent.text = comment.content
            }
        }
    }

    override fun getItemView(parent: ViewGroup): View? {
        binding = LayoutGoodsDetailItemCommentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return binding.root
    }

}